package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sumpu.app.guoguojie.dao.BlogDao;
import cn.sumpu.app.guoguojie.domain.Blog;
import cn.sumpu.app.guoguojie.domain.Ext.BlogExt;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class BlogDaoImpl extends SqlSessionDaoSupport implements BlogDao {
	private static Logger logger = LoggerFactory.getLogger(BlogDaoImpl.class);

	public void insert(Blog blog) throws AppBusinessException {
		try {
			getSqlSession().insert(
					"cn.sumpu.app.guoguojie.persistence.BlogMapper.insert",
					blog);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<BlogExt> getBlogList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.BlogMapper.getBlogList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}

	public void updateBlog(Blog blog) throws AppBusinessException {
		try {
			getSqlSession().update(
					"cn.sumpu.app.guoguojie.persistence.BlogMapper.updateBlog",
					blog);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("update blog failure.", e);
			}
			throw new AppBusinessException("update blog failure.", e);
		}
	}

	public void deleteBlogById(String id) throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.BlogMapper.deleteBlogById",
							id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteBlogById failure.", e);
			}
			throw new AppBusinessException("deleteBlogById failure.", e);
		}
	}

	public Blog getBlogById(String id) throws AppBusinessException {
		try {
			return (Blog) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.BlogMapper.getBlogById",
							id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getBlogById failure.", e);
			}
			throw new AppBusinessException("getBlogById failure.", e);
		}

	}


	@SuppressWarnings("unchecked")
	public List<BlogExt> getFrientFamilyBlog(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.BlogMapper.getFrientFamilyBlog",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<BlogExt> getBlogCountByClass(String member)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.BlogMapper.getBlogCountByClass",
							member);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<BlogExt> getFrientFamilyBlogCountByClass(String member)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.BlogMapper.getFrientFamilyBlogCountByClass",
							member);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}

}
